import request from '@/api/request';

import {toFormData} from 'axios';

const clientId = process.env.VUE_APP_CLIENT_ID;
const clientSecret = process.env.VUE_APP_CLIENT_SECRET;

/**
 * 获取token接口.
 * @param {*} query 参数
 * @returns 返回Promise对象
 */
export const getAccessTokenApi = query => {
    return request({
        url: '/api/auth/oauth2/token',
        method: 'post',
        data: query,
        isLoading: true
    });
};

/**
 * 获取新的 access_token
 */
export const getAccessTokenByRefreshToken = async (refreshToken) => {
    const formData = toFormData({
        client_id: clientId,
        client_secret: clientSecret,
        grant_type: 'refresh_token',
        refresh_token: refreshToken,
    });
    return getAccessTokenApi(formData);
};

/**
 * 用户退出.
 * @param {*} query 参数
 * @returns 返回Promise对象
 */
export const logoutApi = query => {
    return request({
        url: '/api/auth/oauth2/logout',
        method: 'get',
        params: query,
        isLoading: true
    });
};

